package com.cw.lang.common.framework.inteceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.HandlerInterceptor;

import com.cw.lang.common.constants.ControllerFlagConstant;
import com.cw.lang.common.utils.context.ThreadContextStoreUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * 增加一个标记位，只是用于标记进入了controller，用以给{@link LoggingFromHttpMessageConverter}使用
 *
 * @author chenw
 * @version V2.1
 * @since 2.1.0 2019-03-18 20:07
 */
@Slf4j
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ControllerFlagMvcHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        ThreadContextStoreUtil.getInstance()
            .set(ControllerFlagConstant.REQUEST_PATH, request.getRequestURI());
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
        Exception ex) {
        ThreadContextStoreUtil.getInstance()
            .removeKey(ControllerFlagConstant.REQUEST_PATH);
    }

}
